import { Injectable } from '@angular/core';
import { Http, HttpModule, Headers, URLSearchParams, RequestOptions, Jsonp, ResponseContentType } from '@angular/http';
import { environment } from '../../environments/environment';
import { Observable } from 'rxjs/Rx';
import 'rxjs/add/operator/toPromise';
import { Md5 } from "ts-md5/dist/md5";
import 'rxjs/Rx';
import 'rxjs/add/operator/catch';
import { InterceptorService } from 'ng2-interceptors';
import 'rxjs/add/operator/map';
// import 'assets/js/jsencrypt.js';
declare var JSEncrypt;
declare var $: any;
// declare var setPublicKey;
// declare var encrypt;

@Injectable()
export class DataService {
  /**apiURL */
  baseUrl = "api/";
  // baseUrl = "http://192.168.1.234:5000/api/";
  // baseUrl = environment.config.apiUrl;
  /** RSA的私钥*/
  /**dev */
  // tokena = "";
  /**master */
  tokena = "";

  // tokena = environment.config.tokena;

  /**公钥*/
  PUBLIC_KEY = '';
  /**私钥*/
  PRIVATE_KEY = '';
  constructor(
    public http: InterceptorService
  ) {

  }

  //获取验证码
  getCode(mobile) {
    var encrypt = new JSEncrypt();
    // //RSA的公钥
    // encrypt.setPublicKey(this.PUBLIC_KEY);
    // //加密的内容
    // var encryptData = encrypt.encrypt("");
    // console.log(encryptData);

    //RSA的私钥
    encrypt.setPrivateKey(this.PRIVATE_KEY);
    //解密的内容dev
    var tokens = encrypt.decrypt(this.tokena);
    //解密的内容master
    // var tokens= encrypt.decrypt("");


    /**时间戳 */
    let timestam = Math.round(new Date().getTime() / 1000);
    /**4位数据数 */
    let nonce = 0;
    do {
      nonce = Math.floor(Math.random() * 10000);
    } while (nonce < 1000)
    /**token dev */
    let signature: any = Md5.hashStr(tokens + timestam + nonce);
    /**token master */
    //  let signature: any = Md5.hashStr("" + timestam + nonce);
    /**MD5 */
    // console.log(Md5.hashStr(signature));
    //   console.log(signature); 
    let headers = new Headers({ "signature": signature, "timestamp": timestam, "nonce": nonce });
    headers.append("Content-Type", "application/json");

    // let params = new URLSearchParams();
    // params.set("phoneNumber", mobile);
    let options = new RequestOptions({ headers: headers });
    return this.http.get(this.baseUrl + `Login/captchas?phoneNumber=${mobile}`,
      options,
    ).map(res => res.json());
  }

  //帮抢红包
  getRedPacketDetail(mobile, shareListRedId, code) {

    var encrypt = new JSEncrypt();
    // //RSA的公钥
    // encrypt.setPublicKey(this.PUBLIC_KEY);
    // //加密的内容
    // var encryptData = encrypt.encrypt("");
    // console.log(encryptData);

    //RSA的私钥
    encrypt.setPrivateKey(this.PRIVATE_KEY);
    //解密的内容dev
    var tokens = encrypt.decrypt(this.tokena);
    //解密的内容master
    // var tokens= encrypt.decrypt("");


    /**时间戳 */
    let timestam = Math.round(new Date().getTime() / 1000);
    /**4位数据数 */
    let nonce = 0;
    do {
      nonce = Math.floor(Math.random() * 10000);
    } while (nonce < 1000)
    /**token dev */
    let signature: any = Md5.hashStr(tokens + timestam + nonce);
    /**token master */
    //  let signature: any = Md5.hashStr("" + timestam + nonce);
    /**MD5 */
    // console.log(Md5.hashStr(signature));
    //   console.log(signature); 
    let headers = new Headers({ "signature": signature, "timestamp": timestam, "nonce": nonce });
    headers.append("Content-Type", "application/json");
    let options = new RequestOptions({ headers: headers });
    // console.log(options);
    return this.http.get(this.baseUrl + `DuiBa/GetShareDetailRed?phonenumber=${mobile}&sharlistredid=${shareListRedId}&captchas=${code}`, options).map(res => res.json());
  }
  //获取分享者 获取的积分信息
  GetMyRedInfo(userid, orderNumber) {
    var encrypt = new JSEncrypt();
    // //RSA的公钥
    // encrypt.setPublicKey(this.PUBLIC_KEY);
    // //加密的内容
    // var encryptData = encrypt.encrypt("");
    // console.log(encryptData);

    //RSA的私钥
    encrypt.setPrivateKey(this.PRIVATE_KEY);
    //解密的内容dev
    var tokens = encrypt.decrypt(this.tokena);
    //解密的内容master
    // var tokens= encrypt.decrypt("");


    /**时间戳 */
    let timestam = Math.round(new Date().getTime() / 1000);
    /**4位数据数 */
    let nonce = 0;
    do {
      nonce = Math.floor(Math.random() * 10000);
    } while (nonce < 1000)
    /**token dev */
    let signature: any = Md5.hashStr(tokens + timestam + nonce);
    /**token master */
    //  let signature: any = Md5.hashStr("" + timestam + nonce);
    /**MD5 */
    // console.log(Md5.hashStr(signature));
    //   console.log(signature); 
    let headers = new Headers({ "signature": signature, "timestamp": timestam, "nonce": nonce });
    headers.append("Content-Type", "application/json");
    let options = new RequestOptions({ headers: headers });
    return this.http.get(this.baseUrl + `DuiBa/GetMyRedInfo?userid=${userid}&ordernumber=${orderNumber}`,
      options,
    ).map(res => res.json());
  }

  /**来货拉货源分享 */
  SharedGoods(orderid, userid) {
    var pathUrl = window.location.hash;
     /**中文转码 */
     var zhuanma = decodeURIComponent(pathUrl);
     /**匹配？ */
     var wenhao = zhuanma.indexOf("?");
 
     if (wenhao !== -1) {
       /**？的位置 */
       var pathu = zhuanma.substring(wenhao + 1);
       /**分割成数组 */
       var urlarr = pathu.split("&");
       
       /** 签名*/
       var signature = urlarr[3].split("=")[1];
       /** 时间戳*/
       var timestamp = urlarr[4].split("=")[1];
       /** 4位数*/
       var nonce = urlarr[5].split("=")[1];
      }else{
        $.alert("非法访问~");
      }
    let headers = new Headers({ "signature": signature, "timestamp": timestamp, "nonce": nonce });
    headers.append("Content-Type", "application/json");
    let options = new RequestOptions({ headers: headers });
    return this.http.get(this.baseUrl + `Web/GetOrder?orderid=${orderid}&userid=${userid}`,
    options,
    ).map(res => res.json());
  }

}
